package tracksystem;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import managesystem.Buddy;
import managesystem.Student;
import managesystem.StudentManager;
import controlsystem.BadgeController;
import controlsystem.Course;

/**
 * Deze klasse behandeld de informatie dat door de gebruiker ingevuld wordt
 * wanneer er gekozen wordt voor een schoolactiviteit te tracken.
 * 
 * @author Jannes Janssens
 * 
 */
public class TrackSchoolServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private Course course;
	private int pages;
	private int seconds;
	private int minutes;
	private String place;
	private String type;
	private int rating;
	private String comment;
	private String description;
	private ArrayList<Buddy> buddies = new ArrayList<Buddy>();
	private String[] distr;
	private String[] aids;
	private Student currStud;

	protected void getFormInfo(HttpServletRequest req) {
		this.seconds = Integer.parseInt(req.getParameter("seconds"));
		this.minutes = Integer.valueOf(req.getParameter("minutesinseconds"));
		this.place = req.getParameter("place");
		this.type = req.getParameter("type");
		this.rating = Integer.valueOf(req.getParameter("rating"));
		this.comment = req.getParameter("comment");
		this.description = req.getParameter("description");
		// Haal de, eventueel meerdere, buddies op.
		String[] buddies = req.getParameterValues("buddies");
		// Verwijder lege of dubbele entries
		ArrayList<String> listBuds = new ArrayList<String>();
		for (String b : buddies)
			if (!b.isEmpty() && !b.equals("")) {
				for (String s : listBuds) {
					if (!s.equals(b))
						listBuds.add(b);
				}
			}
		this.aids = listBuds.toArray(new String[listBuds.size()]);

		// Omzetten naar buddyObjects
		parseBuddies(buddies, this.currStud);
		// Haal de aids & distr op
		// Haal de aids & distr op
		String[] aids = req.getParameterValues("aids");
		String[] distr = req.getParameterValues("distr");
		// Verwijder lege entries uit deze twee arrays
		ArrayList<String> listAids = new ArrayList<String>();
		for (String s : aids)
			if (!s.isEmpty())
				listAids.add(s);
		this.aids = listAids.toArray(new String[listAids.size()]);

		ArrayList<String> listDistr = new ArrayList<String>();
		for (String s : distr)
			if (!s.isEmpty())
				listDistr.add(s);
		this.distr = listDistr.toArray(new String[listDistr.size()]);

		List<Course> studCourses = this.currStud.getCourses();
		for (Course c : studCourses) {
			if (c.getName().equals(req.getParameter("course"))) {
				this.course = c;
			}
		}
		System.out.println(course.getName());
		this.pages = (!req.getParameter("pages").isEmpty()) ? Integer
				.valueOf(req.getParameter("pages")) : 0;
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		pages = 0;
		HttpSession session = req.getSession();
		this.currStud = (Student) session.getAttribute("currStud");
		getFormInfo(req);

		Activity tempAct = this.currStud.getUnfinishedActivity();
		if (tempAct.equals(null)) {
			/**
			 * Fout! Om een activiteit op te slaan moet er al een tijdelijke
			 * activiteit gemaakt zijn
			 */
			session.setAttribute("error", "Something went terribly wrong!");
			try {
				resp.sendRedirect("/index.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
			return;
		}
		update(tempAct, this.currStud);
		if (!BadgeController.checkBadge(course, this.currStud).isEmpty()) {
			session.setAttribute("message", "Schoolactivity tracked succesfully, you've also received a new badge! you can look them up in your Dashboard.");
		}
		else {
			session.setAttribute("message", "Schoolactivity tracked succesfully");
		}
		session.setAttribute("currStud", this.currStud);
		try {
			resp.sendRedirect("/index.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	protected void update(Activity tempAct, Student currStud) {
		SchoolActivity tempActSA = (SchoolActivity) tempAct;
		tempActSA.setCourse(course);
		tempActSA.setPages(pages);
		tempActSA.setStopDate(seconds+minutes);
		tempActSA.setComment(comment);
		tempActSA.setRating(rating);
		tempActSA.setPlace(place);
		tempActSA.setType(type);
		tempActSA.setDescription(description);
		tempActSA.setBuddies(buddies);
		tempActSA.setAids(aids);
		tempActSA.setDistractions(distr);
		currStud.addActivity(tempActSA);
		StudentManager.add(currStud);// update het student object
	}

	private void parseBuddies(String[] ids, Student currStud) {
		List<Buddy> studBuddies = currStud.getBuddies();
		for (String s : ids) {
			for (Buddy b : studBuddies) {
				if (!(b.equals(null) || b.equals(""))
						&& (!(s.equals(null) || s.equals("") || s.isEmpty()))) {
					if (Integer.valueOf(s) == b.getId()) {
						buddies.add(b);
					}
				}
			}
		}
	}
}